/*
 * @Author: long-xinyu2 2192763423@qq.com
 * @Date: 2025-03-11 18:47:55
 * @LastEditors: long-xinyu2 2192763423@qq.com
 * @LastEditTime: 2025-05-16 14:42:00
 * @FilePath: \algorithm\algorithm-training\2025_3_11.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<math.h>
#include<set>
#include<functional>
#include<bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
 #define int long long
//using i128 = __int128_t;
inline int inc(int x, int v, int mod) { x += v;return x >= mod ? x - mod : x; }//代替取模+
#define endl '\n'
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

const int N = 3830 + 10, M = 1e3 + 10;

inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

int dp[N][2];
int tmp[N][2];
int arr[N];

void solve()
{
    int n,b;
    cin >> n >> b;
    for(int i = 1; i <= n; i ++) cin >> arr[i];
    memset(dp,-0x3f,sizeof dp);
    memset(tmp,-0x3f,sizeof tmp);
    tmp[1][1] = 0;
    tmp[0][0] = 0;
    for(int i = 2; i <= n; i ++)
    {
        for(int j = 0; j <= min(b,i); j ++)
        {
            dp[j][0] = max(tmp[j][0],tmp[j][1]);
            if(j > 0) dp[j][1] = max(tmp[j - 1][0],tmp[j - 1][1] + arr[i]);
        }
        swap(dp,tmp);
    }
    int ans = 0;
    ans = tmp[b][0];
    memset(dp,-0x3f,sizeof dp);
    memset(tmp,-0x3f,sizeof tmp);
    tmp[1][1] = arr[1];
    tmp[0][0] = 0;
    for(int i = 2; i <= n; i ++)
    {
        for(int j = 0; j <= min(b,i); j ++)
        {
            dp[j][0] = max(tmp[j][0],tmp[j][1]);
            if(j > 0) dp[j][1] = max(tmp[j - 1][0],tmp[j - 1][1] + arr[i]);
        }
        swap(dp,tmp);
    }
    ans = max(ans,tmp[b][1]);
    cout << ans << endl;
    
}
signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);
    int tt;
    // cin >> tt;
    // cin.get();
    tt = 1;
    while (tt--)
    {
        solve();
    }
}